package com.autoai.animation.demo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.autoai.animation.demo.recycleview.DividerGridItemDecoration;
import com.autoai.animation.demo.recycleview.DragRecyclerView;
import com.autoai.animation.demo.recycleview.HoldTouchHelper;
import com.autoai.animation.demo.recycleview.ListAdapter;
import com.autoai.animation.demo.recycleview.SampleAdapter;
import com.autoai.animation.demo.recycleview.SampleEntity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Wangjw
 * @date 2020-12-24
 * @brief
 */
public class FourthFragment extends Fragment {

    DragRecyclerView rv_card;
    Button bt_edit_card;
    private List<SampleEntity> data;
    View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.lay_fourth_fragment, container, false);
        rv_card = view.findViewById(R.id.rv_card);
        bt_edit_card = view.findViewById(R.id.bt_edit_card);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initView(view);
    }

    private void initView(View view) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()) {
            @Override
            public boolean isAutoMeasureEnabled() {
                return true;
            }
        };
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_card = (DragRecyclerView) view.findViewById(R.id.rv_card);
        rv_card.addItemDecoration(new DividerGridItemDecoration());
        rv_card.setHasFixedSize(false);
        rv_card.setLayoutManager(linearLayoutManager);
        data = initData();
        rv_card.dragEnable(false)
                .showDragAnimation(true)
                .setDragAdapter(new ListAdapter(getActivity(), data))
                .bindEvent(onItemTouchEvent);
        bt_edit_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initAnimation();
                rv_card.dragEnable(true);
            }
        });
    }

    private void initAnimation() {
        rv_card.scrollToPosition(rv_card.getAdapter().getItemCount() - 1);
        ValueAnimator openAnimator1 = ValueAnimator.ofFloat(rv_card.getWidth(), (rv_card.getWidth() + bt_edit_card.getWidth()));
        openAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) rv_card.getLayoutParams();

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float f = (Float) animation.getAnimatedValue();
                params.width = (int) f;
                rv_card.requestLayout();
            }
        });
        ValueAnimator openAnimator2 = ValueAnimator.ofInt(rv_card.getHeight(), (int) (rv_card.getHeight() * 0.8));
        openAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) rv_card.getLayoutParams();

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int f = (int) animation.getAnimatedValue();
                params.height = (int) f;
                rv_card.requestLayout();
            }
        });
//        ObjectAnimator openAnimator3 = ObjectAnimator.ofFloat(rv_card, "translationX", 0, bt_edit_card.getWidth() / 2);
        ObjectAnimator openAnimator4 = ObjectAnimator.ofFloat(bt_edit_card, "translationX", 0, bt_edit_card.getWidth());
        AnimatorSet animSet = new AnimatorSet();
        animSet.playTogether(openAnimator1, openAnimator2, openAnimator4);
        animSet.setDuration(1000);
        animSet.start();
    }


    private List<SampleEntity> initData() {
        List<SampleEntity> result = new ArrayList<>();
        String[] strings = getResources().getStringArray(R.array.list_array);
        List<String> stringList = (List<String>) Arrays.asList(strings);
        for (String s : stringList) {
            SampleEntity item = new SampleEntity();
            item.setText(s);
            if (stringList.indexOf(s) == 19) {
                item.setDragEnable(false);
                item.setDropEnable(false);
            } else {
                item.setDragEnable(true);
                item.setDropEnable(true);
            }
            result.add(item);
        }
        return result;
    }

    HoldTouchHelper.OnItemTouchEvent onItemTouchEvent = new HoldTouchHelper.OnItemTouchEvent() {
        @Override
        public void onLongPress(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) {
            if (((SampleAdapter) recyclerView.getAdapter()).onItemDrag(position)) {
                ((DragRecyclerView) recyclerView).startDrag(viewHolder);
            }
        }

        @Override
        public void onItemClick(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) {
            String text = data.get(position).getText();
            Toast.makeText(getActivity(), text + ":currentPosition：" + position, Toast.LENGTH_SHORT).show();
        }
    };

}
